package cn.com.text.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.sql.Connection;
import java.sql.PreparedStatement;

import cn.com.text.R;
import cn.com.text.Util.MySQLConnections;

public class UpdataPost extends AppCompatActivity {
    private ImageView imageView;
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Toolbar toolbar;
    private Button button;
    private Connection connection;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_post);
        imageView = (ImageView) findViewById(R.id.imageUp);
        editText1  = (EditText) findViewById(R.id.upAuthor);
        editText2 = (EditText) findViewById(R.id.uptitle);
        editText3 = (EditText) findViewById(R.id.upcontent);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        final String content = intent.getStringExtra("content");
        editText2.setText(title);
        editText3.setText(content);
        button = (Button) findViewById(R.id.buttonUp);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                           connection = MySQLConnections.getConnection("blog");
                            String title1 = editText2.getText().toString();
                            String content1 = editText3.getText().toString();
                            String sql = "update Blog set title='"+title1+"' where content='"+content+"'";
                            PreparedStatement pst;
                            pst = connection.prepareStatement(sql);
                            pst.executeUpdate();
                            sql = "update Blog set content='"+content1+"' where title='"+title1+"'";
                            pst.close();
                            connection.close();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}
